package com.lpf.back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lpf.back.domain.Comment;
import com.lpf.back.domain.Dynamic;
import com.lpf.back.domain.vo.UserDynamicVO;
import com.lpf.back.mapper.CommentMapper;
import com.lpf.back.mapper.DynamicMapper;
import com.lpf.back.service.interfaces.DynamicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.text.DateFormatter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Formatter;
import java.util.List;

@Service
public class DynamicServiceImpl implements DynamicService {

    private static final int PAGESIZE = 10;

    @Autowired
    private DynamicMapper dynamicMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public List<UserDynamicVO> dynamicList() {
        List<UserDynamicVO> userDynamicVOS = dynamicMapper.selectUserDynamicVOList();
        for (UserDynamicVO userDynamicVO : userDynamicVOS) {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            userDynamicVO.setRelDate(format.format(userDynamicVO.getReleaseDate()));

            QueryWrapper<Comment> wrapper = new QueryWrapper<>();
            wrapper.eq("dynamicid", userDynamicVO.getId());
            Integer comments = commentMapper.selectCount(wrapper);
            userDynamicVO.setCommentNum(comments);
        }
        return userDynamicVOS;
    }
}
